//
//  VCLoginViewController.m
//  
//
//  Created by VoyageCN on 16/8/19.
//
//

#import "VCLoginViewController.h"
#import "VCLoginInPutView.h"
#import "VCThirdLoginView.h"
#import "VCRegisterViewController.h"
#import "UIView+Toast.h"

@interface VCLoginViewController ()

@property (nonatomic, strong) VCLoginInPutView *inputView;

@property (nonatomic, strong) VCThirdLoginView *thirdView;

@end

@implementation VCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = MainColor;
    self.edgesForExtendedLayout = 0;
    [self.view addSubview:self.inputView];
    [self.view addSubview:self.thirdView];
    
    __weak typeof(self) weakSelf = self;
    
    [_inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.equalTo(weakSelf.view);
        make.height.equalTo(@170);
    }];
    
    [_thirdView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.inputView.mas_bottom);
        make.left.right.equalTo(weakSelf.view);
    }];
}

- (UIView *)inputView {
    
    if (!_inputView) {
        _inputView = [[VCLoginInPutView alloc] init];
        
        __weak typeof(self) weakSelf = self;
        _inputView.loginMethodBlock = ^(NSDictionary *dic){
            [weakSelf loginMethod:dic];
        };
    }
    return _inputView;
}

- (VCThirdLoginView *)thirdView {
    
    if (!_thirdView) {
        _thirdView = [[VCThirdLoginView alloc] init];
    }
    return _thirdView;
}

- (void)loginMethod:(NSDictionary *)dic {
    
    [self GETRequestWithURL:@"appMember/appLogin.do" param:dic isSuccess:^(id responseObject) {
        //用户不存在
        //密码错误
        //登陆成功
        VCLog(@"responseObject : %@",responseObject);
        if ([responseObject[@"ErrorMessage"] isEqualToString:@"登陆成功"]) {
            [self showTostInView:@"登录成功"];
            [[NSUserDefaults standardUserDefaults] setObject:responseObject forKey:@"ISLOGIN"];
            [self performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
        }else if ([responseObject[@"ErrorMessage"] isEqualToString:@"密码错误"]){
            [self showTostInView:@"密码错误"];
        }else if ([responseObject[@"ErrorMessage"] isEqualToString:@"用户不存在"]){
            [self showTostInView:@"用户不存在"];
        }else{
            [self showTostInView:@"登录失败"];
        }

    } isError:^(NSError *error) {
        VCLog(@"error : %@",error);
    }];
}

- (void)popMyViewController {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
